/***********************************************************************
 * BinTrotter : BitTorrent library/client for the Android operating system.
 *  
 * Copyright (C) 2009, Christopher David Stones (chris.stones@gmail.com)
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 ***********************************************************************/

package org.bintrotter.bdecode;
	
import java.util.Iterator;
import java.io.InputStream;


public class BCodeTree {

	public enum NodeType  {
		String,
		Integer,
		List,
		Dictionary
	}

	public interface NodeData {
		NodeType getType();
		long	 getLong();
		byte[]	 getString();
	}

	class LeafNode implements NodeData {
		public LeafNode(byte[] bytes) {
			mBytes = bytes;
			mInt = 0;
		}
		public LeafNode(long i) {
			mBytes = null;
			mInt = i;
		}
		public NodeType getType() {
			if(mBytes == null)
				return NodeType.Integer;
			return NodeType.String;	
		}
		public long getLong() {
			return mInt;
		}
		public byte[] getString() {
			return mBytes;
		}
		private byte[]  mBytes;
		private long	mInt;
	}
	
	class BranchNode implements NodeData {
		
		public BranchNode( NodeType nodeType ) {
			mNodeType = nodeType;
		}
		public NodeType getType() {
			return mNodeType;	
		}
		public long getLong() {
			return 0;
		}
		public byte[] getString() {
			return null;
		}
		private NodeType mNodeType;
	}
	
	public BCodeTree( InputStream is ) throws Throwable {
		
		BuildTree( new Tokenise(is) );
	}
	
	public BCodeTree( String fileName) throws Throwable {
		
		BuildTree(  new Tokenise(fileName) );
	}
	
	private void BuildTree( Tokenise tokens ) throws Throwable {
			
		 Iterator<Long> IntList = tokens.longList.iterator();
		 Iterator<byte[]>  StrList = tokens.stringList.iterator();
		 
		 Node<NodeData> headNode = rootNode = new Node<NodeData>();
		 
		for(Iterator<Tokens> it = tokens.tokenList.iterator(); it.hasNext(); ) {
			
			switch( it.next() ) {
			case Integer:
				new Node<NodeData>(headNode, new LeafNode(IntList.next().longValue() ));
				break;
				
			case String:
				new Node<NodeData>(headNode, new LeafNode(StrList.next()));
				break;
				
			case List:
				headNode = new Node<NodeData>(headNode, new BranchNode(NodeType.List));
				break;
				
			case Dictionary:
				headNode = new Node<NodeData>(headNode, new BranchNode(NodeType.Dictionary));				
				break;
			case End:
				
				headNode = headNode.parent;
				break;
			}
		}
	}
	
	public Node<NodeData> rootNode;
}


